home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue66 / SQLComp / DMSQLCriteria.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-10-11  |  3.7 KB  |  149 lines

  1. unit DMSQLCriteria;
  2.  
  3. {
  4.   Other criteria you can define;
  5.   - TDMSQLCheckBox, for Boolean fields (which must also be added to the
  6.     set of supported field types).
  7.   - TDMSQLComboBox (similar to TMDSQLListBox).
  8.   - TDMSQLLookupListBox (a TDMSQLListBox bound to a DataSet).
  9.   - TDMSQLLookupComboBox (a TDMSQLComboBox bound to a DataSet).
  10.   - TDMSQLRadioGroup (similar to TDMSQLListBox).
  11.   - TDMSQLLookupRadioGroup (a TDMSQLRadioGroup bound to a DataSet).
  12.  
  13.   Possible support components:
  14.   - A ComboBox linked to a criterion, used to set its SQLOperator property.
  15.   - A RadioGroup or ListBox with the same function.
  16.   - A ComboBox, or ListBox etc. to set the value of the DataField property.
  17. }
  18.  
  19. interface
  20.  
  21. uses
  22.   Classes, StdCtrls, DB, DMSQLBase;
  23.  
  24. type
  25.   // An Edit box criterion.
  26.   TDMSQLEdit = class(TEdit, IDMSQLCriterion, IDMSQLCriterionData)
  27.   private
  28.     FCriterion: TDMSQLSingleCriterion;
  29.     function GetSQLValue: Variant;
  30.     procedure ClearSQL;
  31.   protected
  32.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  33.   public
  34.     constructor Create(AOwner: TComponent); override;
  35.     destructor Destroy; override;
  36.   published
  37.     property Criterion: TDMSQLSingleCriterion read FCriterion write FCriterion implements IDMSQLCriterion;
  38.   end;
  39.  
  40.   // A ListBox multiple criterion.
  41.   TDMSQLListBox = class(TListBox, IDMSQLCriterion, IDMSQLCriterionData)
  42.   private
  43.     FCriterion: TDMSQLMultipleCriterion;
  44.     function GetSQLValue: Variant;
  45.     procedure ClearSQL;
  46.   protected
  47.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  48.   public
  49.     constructor Create(AOwner: TComponent); override;
  50.     destructor Destroy; override;
  51.   published
  52.     property Criterion: TDMSQLMultipleCriterion read FCriterion write FCriterion implements IDMSQLCriterion;
  53.   end;
  54.  
  55. implementation
  56.  
  57. uses
  58.   SysUtils, DMSQLUtils;
  59.  
  60. { TDMSQLEdit }
  61.  
  62. constructor TDMSQLEdit.Create(AOwner: TComponent);
  63. begin
  64.   inherited Create(AOwner);
  65.   FCriterion := TDMSQLSingleCriterion.Create(Self);
  66. end;
  67.  
  68. destructor TDMSQLEdit.Destroy;
  69. begin
  70.   FCriterion.Free;
  71.   inherited;
  72. end;
  73.  
  74. function TDMSQLEdit.GetSQLValue: Variant;
  75. begin
  76.   Result := SQLStringToVariant(Text);
  77. end;
  78.  
  79. procedure TDMSQLEdit.Notification(AComponent: TComponent; Operation: TOperation);
  80. begin
  81.   inherited;
  82.   FCriterion.CustomNotification(Acomponent, Operation);
  83. end;
  84.  
  85. procedure TDMSQLEdit.ClearSQL;
  86. begin
  87.   Clear;
  88. end;
  89.  
  90. { TDMSQLListBox }
  91.  
  92. constructor TDMSQLListBox.Create(AOwner: TComponent);
  93. begin
  94.   inherited;
  95.   FCriterion := TDMSQLMultipleCriterion.Create(Self);
  96. end;
  97.  
  98. destructor TDMSQLListBox.Destroy;
  99. begin
  100.   FCriterion.Free;
  101.   inherited;
  102. end;
  103.  
  104. function TDMSQLListBox.GetSQLValue: Variant;
  105. var
  106.   VarIndex: Integer;
  107.   g: Integer;
  108. begin
  109.   if not MultiSelect then begin
  110.     Result := VarArrayCreate([1, 1], varVariant);
  111.     // Single selection: return a single value.
  112.     if ItemIndex < 0 then
  113.       Result[1] := SQLStringToVariant('')
  114.     else
  115.       Result[1] := SQLStringToVariant(Items[ItemIndex]);
  116.   end
  117.   else begin
  118.     // Multiple selection: return an array.
  119.     Result := VarArrayCreate([1, SelCount], varVariant);
  120.     VarIndex := 1;
  121.     for g := 0 to Pred(Items.Count) do
  122.       if Selected[g] then begin
  123.         Result[VarIndex] := SQLStringToVariant(Items[g]);
  124.         Inc(VarIndex);
  125.       end;
  126.   end;
  127. end;
  128.  
  129. procedure TDMSQLListBox.Notification(AComponent: TComponent; Operation: TOperation);
  130. begin
  131.   inherited;
  132.   FCriterion.CustomNotification(Acomponent, Operation);
  133. end;
  134.  
  135. procedure TDMSQLListBox.ClearSQL;
  136. var
  137.   g: Integer;
  138. begin
  139.   if MultiSelect then begin
  140.     for g := 0 to Pred(Items.Count) do
  141.       if Selected[g] then
  142.         Selected[g] := False;
  143.   end
  144.   else
  145.     ItemIndex := -1;
  146. end;
  147.  
  148. end.
  149.